//
//  XYDownloadCell.swift
//  XYReadBook
//
//  Created by fox on 2017/10/25.
//  Copyright © 2017年 tsaievan. All rights reserved.
//

import UIKit


/// 下载页面
class XYDownloadCell: UITableViewCell {
    var isSelectedBtn:Bool? {
        didSet {
            if isSelectedBtn == true {
                selectedBtn.isSelected = true
            }else {
                 selectedBtn.isSelected = false
            }
        }
    }
    // let 常量 var 变量
    // 常量  名  : 分隔告诉他是那种类型 String = "" 默认值


    var model:Chapters? {
        didSet {
            if let model = model {
                let curentLink = model.link.urlEncode!
                let urlKey = BaseUrl.chapter.rawValue + curentLink

                let iscontains = HTTPCache.xwj_containsObject(forKey: urlKey)
                if iscontains == true {
                    endDownloadLab.isHidden = false
                    selectedBtn.isHidden = true
                }else {
                     endDownloadLab.isHidden = true
                      selectedBtn.isHidden = false
                }
                 chapterLab.text = model.title
            }
        }
    }
    var selectedChapter:((_ chapter:UIButton) ->Void)?
    let chapterLab = UILabel()
    let selectedBtn = UIButton()
    let lineView = UIView()
    var endDownloadLab = UILabel()

    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override func layoutSubviews() {
        super.layoutSubviews()
        selectedBtn.frame = CGRect.init(x: ScreenWidth - 54, y: 12, width: 40, height: 24)
        lineView.frame = CGRect.init(x: 0, y: self.height - 1, width: self.width, height: 1)
        chapterLab.frame = CGRect.init(x: 36, y: 12, width: ScreenWidth - 40, height: 24)
        endDownloadLab.frame =  CGRect.init(x: ScreenWidth - 74, y: 12, width: 60, height: 24)
    }
    func setupUI() {
        addSubview(lineView)
        addSubview(chapterLab)
        addSubview(selectedBtn)
        addSubview(endDownloadLab)
        endDownloadLab.text = "已下载"
        endDownloadLab.textColor = UIColor.gray
        endDownloadLab.isHidden = true

        lineView.backgroundColor = UIColor.init(red: 243/255.0, green: 243/255.0, blue: 243/255.0, alpha: 1)
        selectedBtn.addTarget(self, action: #selector(clickChapter(sender:)), for: .touchUpInside)
        selectedBtn.backgroundColor = UIColor.clear
        selectedBtn.setImage(UIImage.init(named: "payPick"), for: .normal)
        selectedBtn.setImage(UIImage.init(named: "payPick_selected"), for: .selected)

        // 再此判断章节有没有下载过 if true 隐藏 显示   已经下载

        

        if let isSelectedBtn = isSelectedBtn {
            selectedBtn.isSelected = isSelectedBtn;
        }
    }
    func clickChapter(sender:UIButton) {
        selectedBtn.isSelected = !selectedBtn.isSelected
        selectedChapter?(sender)

    }
}
